//index.js
//获取应用实例
const getCurrentDate = require('../../utils/util').getCurrentDate

const baseUrl = require('../../config').baseUrl
const app = getApp()

Page({
    data: {
        userInfo: {},
        hasUserInfo: false,
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        isSelect: false,
        selMsg: '请选择查询单位',
        imgSrc: [],//整改后图片
        befimgSrc: ['/images/repair.png'],
        btnShow: true,
        hasModified: false, //是否整改
        hideId: '', // 隐患id
        hideInfo: {}, // 隐患详情
        companyList: [], // 单位列表
        deptid: '', // 被检查单位的id
        description: '', // 整改情况
        page: '' // 从隐患台账进来的还是从隐患整改进来的 1表示隐患台账 2表示隐患整改
    },
    onLoad: function (options) {
        wx.showShareMenu({
            withShareTicket: true
        })
        this.setData({
            hideId: options.hideId,
            page: options.page
        })
        this.getHideDetail()
        this.getCompany()
    },
    /**
     * 获取隐患详情
     */
    getHideDetail () {
        let This = this
        wx.request({
            url: baseUrl,
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            data: {
                data: JSON.stringify({
                    business: "GetHiddenInfo",
                    data: this.data.hideId
                })
            },
            method: 'POST',
            success (res) {
                if (res.data.code === 0) {
                    let hideInfo = res.data.data
                    let resolvestarttime = hideInfo.resolvestarttime.split(' ')[0]
                    let resolveendtime = hideInfo.resolveendtime.split(' ')[0]
                    This.setData({
                        hideInfo: res.data.data,
                        'hideInfo.resolvestarttime': resolvestarttime,
                        'hideInfo.resolveendtime': resolveendtime
                    })
                } else {
                    wx.showToast({
                        title: res.data.info,
                        icon: 'none',
                        duration: 1000
                    })
                }
            },
            fail: (error) => {
                wx.showToast({
                    title: error.data.info,
                    icon: 'none',
                    duration: 1000
                })
            }
        })
    },
    /**
     *  获取单位列表
     */
    getCompany (account) {
        let This = this
        wx.request({
            url: baseUrl,
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            data: {
                data: JSON.stringify({
                    business: "GetDeptInfoList",
                    data: {
                        account: account ? account : app.globalData.account
                    }
                })
            },
            method: 'POST',
            success (res) {
                if (res.data.code === 0) {
                    This.setData({
                        companyList: This.data.companyList.concat(res.data.data)
                    })
                } else {
                    wx.showToast({
                        title: res.data.info,
                        icon: 'none',
                        duration: 1000
                    })
                }
            },
            fail: (error) => {
                wx.showToast({
                    title: error.data.info,
                    icon: 'none',
                    duration: 1000
                })
            }
        })
    },
    /**
     * 切换被检查单位
     */
    changeCompany (e) {
        this.setData({
            'deptid': this.data.companyList[e.detail.value].id,
            'hideInfo.checkdeptname': this.data.companyList[e.detail.value].departname,
        })
    },
    /**
     * 隐患描述内容修改
     */
    changeDescription (e) {
        this.setData({
            'hideInfo.description': e.detail.value
        })
    },
    /**
     * 隐患整改内容修改
     */
    changeMeasure (e) {
        this.setData({
            'hideInfo.measure': e.detail.value
        })
    },
    bindShowMsg: function () {
        this.setData({
            isSelect: !this.data.isSelect
        })
    },
    mySelect: function (e) {
        var name = e.currentTarget.dataset.name
        this.setData({
            selMsg: name,
            isSelect: false
        })
    },
    // 上传图片
    chooseImage: function () {
        var _this = this
        wx.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success (res) {
                // tempFilePath可以作为img标签的src属性显示图片
                let imgUrl = res.tempFilePaths[0]
                wx.uploadFile({
                    url: baseUrl,
                    filePath: res.tempFilePaths[0],
                    name: 'file',
                    header: {
                        "content-type": "application/x-www-form-urlencoded"
                    },
                    formData: {
                        data: JSON.stringify({
                            business: "WxUploadFile",
                            data: app.globalData.account
                        })
                    },
                    success (res) {
                        res.data = JSON.parse(res.data)
                        if (res.data.code === 0) {
                            _this.setData({
                                "imgSrc": _this.data.imgSrc.concat({ imgUrl: imgUrl, fileid: res.data.fileid })
                            })
                            console.log(_this.data.imgSrc)
                        }
                    }
                })
            }
        })
    },
    //删除图片
    deleteImg: function (e) {
        this.data.imgSrc.splice(e.currentTarget.dataset.index, 1)
        this.setData({
            imgSrc: this.data.imgSrc
        })
        // console.log(this.imgSrc)
    },
    /**
     * 预览整改前的图片
     */
    previewBeforeImg (e) {
        var currentSrc = e.currentTarget.dataset.src
        wx.previewImage({
            current: currentSrc,     //当前图片地址
            urls: this.data.hideInfo.problemimgs
        })
    },
    /**
     * 预览整改后的图片
     */
    previewAfterImg (e) {
        var currentSrc = e.currentTarget.dataset.src
        wx.previewImage({
            current: currentSrc,     //当前图片地址
            urls: this.data.hideInfo.resolveimgs
        })
    },
    /**
     * 预览上传图片
     */
    previewImg: function (e) {
        var currentSrc = e.currentTarget.dataset.src
        var imgArr = []
        this.data.imgSrc.forEach(item => {
            imgArr.push(item.imgUrl)
        });
        wx.previewImage({
            current: currentSrc,     //当前图片地址
            urls: imgArr
        })
    },
    changeDescription (e) {
        this.setData({
            'description': e.detail.value
        })
    },
    //完成整改
    hascompleted () {
        if (this.data.imgSrc.length == 0) {
            wx.showToast({
                title: '请上传整改后的图片',
                icon: 'none',
                duration: 1000
            })
            return false
        } else if (this.data.description === '') {
            wx.showToast({
                title: '请输入整改情况',
                icon: 'none',
                duration: 1000
            })
            return false
        }
        let This = this
        let problemimgs = ''
        this.data.imgSrc.forEach(item => {
            if (problemimgs) {
                problemimgs += `,${item.fileid}`
            } else {
                problemimgs += item.fileid
            }
        })
        wx.request({
            url: baseUrl,
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            data: {
                data: JSON.stringify({
                    business: "ResolveHidden",
                    data: {
                        account: app.globalData.account,
                        deptid: this.data.deptid, // 被检查单位id

                        description: this.data.description, // 整改情况
                        hiddendangerid: this.data.hideId, // 隐患ID
                        resolvetime: getCurrentDate(), // 整改时间
                        problemimgs: problemimgs // 整改前图片ID,多张用逗号分隔
                    }
                })
            },
            method: 'POST',
            success (res) {
                if (res.data.code === 0) {
                    wx.showToast({
                        title: '整改成功',
                        icon: 'none',
                        duration: 1000
                    })
                    if (This.data.page == 1) {
                        setTimeout(() => {
                            wx.switchTab({
                                url: '/pages/hiddenaccount/hiddenaccount'
                            })
                        }, 1000)
                    } else {
                        setTimeout(() => {
                            wx.switchTab({
                                url: '/pages/hiddenmodify/hiddenmodify'
                            })
                        }, 1000)
                    }
                } else {
                    wx.showToast({
                        title: res.data.info,
                        icon: 'none',
                        duration: 1000
                    })
                }
            },
            fail: (error) => {
                wx.showToast({
                    title: error.data.info,
                    icon: 'none',
                    duration: 1000
                })
            }
        })
    }
})
